<template>
  <div class="test-container">
    <h2>用户信息存储测试</h2>
    
    <div class="test-section">
      <h3>当前用户信息</h3>
      <div v-if="userInfo" class="user-info">
        <p><strong>ID:</strong> {{ userInfo.id }}</p>
        <p><strong>用户名:</strong> {{ userInfo.username }}</p>
        <p><strong>邮箱:</strong> {{ userInfo.email }}</p>
        <p><strong>头像:</strong> {{ userInfo.avatarUrl || '无' }}</p>
        <p><strong>简介:</strong> {{ userInfo.bio || '无' }}</p>
      </div>
      <div v-else class="no-user">
        <p>未登录，请先登录</p>
        <button @click="$router.push('/login')" class="btn">去登录</button>
      </div>
    </div>

    <div class="test-section">
      <h3>Token信息</h3>
      <p><strong>Token:</strong> {{ token || '无' }}</p>
      <p><strong>登录状态:</strong> {{ isLoggedIn ? '已登录' : '未登录' }}</p>
    </div>

    <div class="test-section">
      <h3>测试操作</h3>
      <button @click="refreshUserInfo" class="btn">刷新用户信息</button>
      <button @click="clearUserInfo" class="btn btn-danger">清除用户信息</button>
      <button @click="testUpdateAvatar" class="btn">测试更新头像</button>
    </div>

    <div class="test-section">
      <h3>头像预览</h3>
      <div v-if="userInfo && userInfo.avatarUrl" class="avatar-preview">
        <img :src="userInfo.avatarUrl" alt="用户头像" class="avatar" />
      </div>
      <div v-else class="no-avatar">
        <p>暂无头像</p>
      </div>
    </div>
  </div>
</template>

<script>
import UserStorage from '@/utils/userStorage'

export default {
  data() {
    return {
      userInfo: null,
      token: null,
      isLoggedIn: false
    }
  },
  mounted() {
    this.refreshUserInfo()
  },
  methods: {
    refreshUserInfo() {
      this.userInfo = UserStorage.getUserInfo()
      this.token = UserStorage.getToken()
      this.isLoggedIn = UserStorage.isLoggedIn()
    },
    clearUserInfo() {
      UserStorage.clearUserInfo()
      this.refreshUserInfo()
      alert('用户信息已清除')
    },
    testUpdateAvatar() {
      const testAvatarUrl = 'http://localhost:8280/uploads/avatars/test-avatar.jpg'
      UserStorage.updateAvatarUrl(testAvatarUrl)
      this.refreshUserInfo()
      alert('测试头像已更新')
    }
  }
}
</script>

<style scoped>
.test-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.test-section {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.test-section h3 {
  margin-top: 0;
  color: #333;
  border-bottom: 2px solid #42b983;
  padding-bottom: 10px;
}

.user-info p {
  margin: 8px 0;
  padding: 8px;
  background: #f5f5f5;
  border-radius: 4px;
}

.no-user {
  text-align: center;
  color: #666;
}

.btn {
  background: #42b983;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  margin-right: 10px;
  margin-bottom: 10px;
}

.btn:hover {
  background: #3aa876;
}

.btn-danger {
  background: #ff4d4f;
}

.btn-danger:hover {
  background: #ff7875;
}

.avatar-preview {
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  border: 3px solid #42b983;
}

.no-avatar {
  text-align: center;
  color: #666;
  padding: 20px;
}
</style>